export default {
  // 下载Excel
  excel(res, fileName) {
    this.download0(res, fileName, 'application/vnd.ms-excel', '.xlsx')
  },
  // 下载docx
  docx(res, fileName) {
    this.download0(res, fileName, 'application/msexcel', 'docx')
  },
  // 下载Word
  word(res, fileName) {
    this.download0(res, fileName, 'application/msword')
  },
  // 下载Zip
  zip(res, fileName) {
    this.download0(res, fileName, 'application/zip')
  },
  // 下载Html
  html(res, fileName) {
    this.download0(res, fileName, 'application/html')
  },
  // 下载Markdown
  markdown(res, fileName) {
    this.download0(res, fileName, 'application/markdown')
  },
  download0(res, fileName, mineType, suffix) {
    const disposition = res.headers['content-disposition']
    if (!fileName) {
      fileName = disposition ? disposition.substring(disposition.indexOf('=') + 1, disposition.indexOf('.')) : '下载文件'
      fileName = decodeURI(fileName)
      const extName = disposition.substring(disposition.lastIndexOf('.') + 1)
      if (extName) {
        fileName = fileName + '.' + extName
      } else {
        fileName = fileName + suffix
      }
    }
    const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = fileName
    link.style.display = 'none'
    document.body.appendChild(link)
    link.click()
    URL.revokeObjectURL(link.href)
    document.body.removeChild(link)
  }
}
